package com.example.prettyprint;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ExtractUzemniJednotky extends DefaultHandler
{
	//private final ExtractUzemniJednotkaHandler mObceHandler;
	//private final ExtractUzemniJednotkaHandler mCastiObciHandler;
	//private final ExtractUzemniJednotkaHandler mKatastralniUzemiHandler;
	//private final ExtractUzemniJednotkaHandler mZsjHandler;
	
	private final List<ExtractUzemniJednotkaHandler> mHandlers;
	
	private final String OBCE_ELEMENT_QNAME = "vf:Obce";
	private final String CASTI_OBCI_ELEMENT_QNAME = "vf:CastiObci";
	private final String KATASTRALNI_UZEMI_ELEMENT_QNAME = "vf:KatastralniUzemi";
	private final String ZSJ_ELEMENT_QNAME = "vf:Zsj";
	
	private final String OBCE_FILENAME = "obce.xml";
	private final String CASTI_OBCI_FILENAME = "castiObci.xml";
	private final String KATASTRALNI_UZEMI_FILENAME = "katastralniUzemi.xml";
	private final String ZSJ_FILENAME = "zsj.xml";
	
	public ExtractUzemniJednotky(String outputDirectoryPath) throws FileNotFoundException
	{
		//this.mObceHandler = new ExtractUzemniJednotkaHandler(OBCE_ELEMENT_QNAME, outputDirectoryPath, OBCE_FILENAME);
		//this.mObceHandler = new ExtractUzemniJednotkaHandler(CASTI_OBCI_ELEMENT_QNAME, outputDirectoryPath, CASTI_OBCI_FILENAME);
		//this.mObceHandler = new ExtractUzemniJednotkaHandler(KATASTRALNI_UZEMI_ELEMENT_QNAME, outputDirectoryPath, KATASTRALNI_UZEMI_FILENAME);
		//this.mObceHandler = new ExtractUzemniJednotkaHandler(ZSJ_ELEMENT_QNAME, outputDirectoryPath, ZSJ_FILENAME);
		
		this.mHandlers = new ArrayList<ExtractUzemniJednotkaHandler>();
		this.mHandlers.add(new ExtractUzemniJednotkaHandler(OBCE_ELEMENT_QNAME, outputDirectoryPath, OBCE_FILENAME));
		this.mHandlers.add(new ExtractUzemniJednotkaHandler(CASTI_OBCI_ELEMENT_QNAME, outputDirectoryPath, CASTI_OBCI_FILENAME));
		this.mHandlers.add(new ExtractUzemniJednotkaHandler(KATASTRALNI_UZEMI_ELEMENT_QNAME, outputDirectoryPath, KATASTRALNI_UZEMI_FILENAME));
		this.mHandlers.add(new ExtractUzemniJednotkaHandler(ZSJ_ELEMENT_QNAME, outputDirectoryPath, ZSJ_FILENAME));
	}
	
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
	}
	
	public void startElement(
	  String namespaceURI,
	  String localName, // simple name
	  String qName, // qualified name
	  Attributes attrs) throws SAXException {
	  	
		for(ExtractUzemniJednotkaHandler handler : this.mHandlers)
		{
			handler.startElement(namespaceURI, localName, qName, attrs);
		}
	}

	public void endElement(String uri, String localName, String qName) throws SAXException
	{
		for(ExtractUzemniJednotkaHandler handler : this.mHandlers)
		{
			handler.endElement(uri, localName, qName);
		}
	}

	public void characters(char buf[], int offset, int len) throws SAXException
	{
		for(ExtractUzemniJednotkaHandler handler : this.mHandlers)
		{
			handler.characters(buf, offset, len);
		}
	}
  }
